package com.kgcx.coupon.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.client.order.OrderClient;
import com.kgcx.common.client.order.pojo.Order;
import com.kgcx.common.eum.OrderEnum;
import com.kgcx.common.utils.DateTimeUtils;
import com.kgcx.coupon.domain.Billing;
import com.kgcx.coupon.domain.Comment;
import com.kgcx.coupon.mapper.CommentMapper;
import com.kgcx.coupon.rabbitmq.producer.OpcProducer;
import com.kgcx.dbutils.utils.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 何凯
 * @version 0.1
 * @description
 * @time 2017/12/7 15:07
 * @since 0.1
 */
@Service
public class CommentService {

    public static Logger logger = LoggerFactory.getLogger(CommentService.class);
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private OpcProducer opcProducer;
    @Autowired
    private OrderClient orderClient;
    /**
     * 分页查询评价信息
     * @param rd
     * @return
     */
    public List<Comment> queryCommentWithPage(RequestData rd){
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        List<Comment> list = commentMapper.selectCommentList(rd);
        for (Comment com :list){
            opcProducer.sendComment(com);
        }
        return list;

    }

    /**
     * 用户评价新增
     * @return
     */
    public Integer addComment(String orderNo,Integer userId, Integer driverId,Float serviceScore){
        Comment comment = new Comment();
        comment.setCommentContent("好评！");
        comment.setOrderNo(orderNo);
        comment.setDriverId(driverId);
        comment.setUserId(userId);
        comment.setServiceScore(serviceScore);
        comment.setDriverScore(5.0f);
        comment.setVehicleScore(5.0f);
        long dateTime =  DateTimeUtils.convertNumberSec(LocalDateTime.now());
        comment.setEvaluateTime(dateTime);
        Integer one = commentMapper.insert(comment);
        try {
            Order order = new Order();
            order.setOrderNo(orderNo);
            order.setOrderState(OrderEnum.COMMENTED);
            order.setPlacingUserId(Long.valueOf(userId));
            orderClient.update(order);
        } catch (Exception e){
            logger.error("orderNo:"+orderNo+"comment update order error",e);
        }
        try {
         //TODO 发送消息给监管平台
            opcProducer.sendComment(comment);
         } catch (Exception e){
            logger.error("orderNo:"+orderNo+"comment send comment error",e);
        }
       return one;

    }


    public List<Comment> selectByOrderNo(String orderNo) {
        List<Comment> list=commentMapper.selectByOrderNo(orderNo);
        return list;
    }




}
